﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SiGeSIB.Data.Common;
using System.Data;

namespace SiGeSIB.GUI.Common.DedaGrid.Filter
{
	public class DedaGrigFilterCondition
	{
		private EDedaGridFilterLogicalConnector _ConditionConnector;
		private ELogicalCondition _LogicalCondition;
		private DataColumn _Column;
		private object _Value;

		public DedaGrigFilterCondition(DataColumn columnName, ELogicalCondition logicalCondition, object value)
			: this(EDedaGridFilterLogicalConnector.Unknown, columnName, logicalCondition, value) { }
		public DedaGrigFilterCondition(EDedaGridFilterLogicalConnector conditionConnector, DataColumn column, ELogicalCondition logicalCondition, object value)
		{
			_ConditionConnector = conditionConnector;
			_Column = column;
			_LogicalCondition = logicalCondition;
			_Value = value;
		}

		public override string ToString()
		{
			string result = string.Empty;

			result += 
				_ConditionConnector != EDedaGridFilterLogicalConnector.Unknown ?
					" " + _ConditionConnector.GetDescription() + " "
					: "";
			result += "["+_Column.ColumnName + "] ";

			Type columnType = _Column.DataType;
			string valueEncloser = "'";

			if (columnType == typeof(DateTime))
				valueEncloser = "#";

			result += 
				_LogicalCondition != ELogicalCondition.CONTAINS ? 
					_LogicalCondition.GetDescription() + " {0}{1}{0}"
					: "LIKE '%{1}%'";
			result = string.Format(result, valueEncloser, _Value.ToString());

			return result;
		}
	}
}
